import axios from 'axios'
import { message, notification } from 'ant-design-vue'

axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.post['Content-Type'] = 'text/plain;charset=UTF-8'
const service = axios.create({
 // baseURL: 'https://music-api.heheda.top/',
  baseURL: 'http://192.168.0.248:3000/api',
  timeout: 10000,
})

// 请求拦截 可在请求头中加入token等
service.interceptors.request.use(
  (config) => {
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截 对响应消息作初步的处理
service.interceptors.response.use(
  (resp) => {
    if (resp.data) {
      if (resp.data.code !== 200) {
        message.error({
          content: resp.data.message,
          duration: 5,
        })
      }
      return resp.data
    } else {
      return resp
    }
  },
  (error) => {
    console.log('error', error)
    if (error.response) {
      switch (error.response.states) {
        case 400: {
          if (
            error.response &&
            error.response.data &&
            error.response.data.message
          ) {
            notification.error({
              message: '400错误',
              description: error.response.data.message,
              duration: 5,
              closable: true,
            })
          }
          break
        }
      }
    }
  }
)

export default service
